package cn.iocoder.yudao.module.exam.service.knowledgegraph;

import cn.hutool.core.collection.CollUtil;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import cn.iocoder.yudao.module.exam.controller.admin.knowledgegraph.vo.*;
import cn.iocoder.yudao.module.exam.dal.dataobject.knowledgegraph.KnowledgeGraphDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;

import cn.iocoder.yudao.module.exam.dal.mysql.knowledgegraph.KnowledgeGraphMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertList;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.diffList;
import static cn.iocoder.yudao.module.exam.enums.ExamErrorCodeConstants.KNOWLEDGE_GRAPH_NOT_EXISTS;

/**
 * 知识图谱 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class KnowledgeGraphServiceImpl implements KnowledgeGraphService {

    @Resource
    private KnowledgeGraphMapper knowledgeGraphMapper;

    @Override
    public Integer createKnowledgeGraph(KnowledgeGraphSaveReqVO createReqVO) {
        // 插入
        KnowledgeGraphDO knowledgeGraph = BeanUtils.toBean(createReqVO, KnowledgeGraphDO.class);
        knowledgeGraphMapper.insert(knowledgeGraph);

        // 返回
        return knowledgeGraph.getId();
    }

    @Override
    public void updateKnowledgeGraph(KnowledgeGraphSaveReqVO updateReqVO) {
        // 校验存在
        validateKnowledgeGraphExists(updateReqVO.getId());
        // 更新
        KnowledgeGraphDO updateObj = BeanUtils.toBean(updateReqVO, KnowledgeGraphDO.class);
        knowledgeGraphMapper.updateById(updateObj);
    }

    @Override
    public void deleteKnowledgeGraph(Integer id) {
        // 校验存在
        validateKnowledgeGraphExists(id);
        // 删除
        knowledgeGraphMapper.deleteById(id);
    }

    @Override
        public void deleteKnowledgeGraphListByIds(List<Integer> ids) {
        // 删除
        knowledgeGraphMapper.deleteByIds(ids);
        }


    private void validateKnowledgeGraphExists(Integer id) {
        if (knowledgeGraphMapper.selectById(id) == null) {
            throw exception(KNOWLEDGE_GRAPH_NOT_EXISTS);
        }
    }

    @Override
    public KnowledgeGraphDO getKnowledgeGraph(Integer id) {
        return knowledgeGraphMapper.selectById(id);
    }

    @Override
    public PageResult<KnowledgeGraphDO> getKnowledgeGraphPage(KnowledgeGraphPageReqVO pageReqVO) {
        return knowledgeGraphMapper.selectPage(pageReqVO);
    }

}